package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.MenuMapper;
import com.hqyj.pojo.Menu;
import com.hqyj.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;


@Service
public class MenuServiceImpl implements MenuService {

    //注入mapper
    @Autowired(required = false)
    MenuMapper menuMapper;

    @Override
    public HashMap<String, Object> select(Menu a) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义分页对象
        Page<Menu> page = new Page<>(a.getPage(),a.getRow());

        //条件构造器
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(a.getDishId()!=null,"dish_id",a.getDishId());
        queryWrapper.like(a.getDishName()!=null&&!a.getDishName().equals(""),"dish_name",a.getDishName());

        //查询数据库
        Page<Menu> aList =menuMapper.selectPage(page,queryWrapper);
        //当前页集合
        map.put("list",aList.getRecords());
        //总页数
        map.put("totalPage",aList.getPages());
        //总条数
        map.put("total",aList.getTotal());
        //当前页
        map.put("curPage",a.getPage());
        if(a.getPage()==1){
            //上一页
            map.put("prePage",1);

        }else{
            map.put("prePage",a.getPage()-1);

        }
        if(a.getPage()==aList.getPages()){
            //下一页
            map.put("nextPage",aList.getPages());

        }else{
            map.put("nextPage",a.getPage()+1);

        }
        //每页显示条数
        map.put("row",a.getRow());

        return map;
    }

    @Override
    public HashMap<String, Object> add(Menu a) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //新增
        int num = menuMapper.insert(a);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Menu a) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        //修改
        int num = menuMapper.updateById(a);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer a) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //删除
        int num = menuMapper.deleteById(a);
        if(num>0){
            map.put("info","删除成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> tList(Menu m) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义分页对象
        Page<Menu> page = new Page<>();

        //条件构造器
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(m.getDishId()!=null,"dish_id",m.getDishId());

        //查询数据库
        Page<Menu> aList =menuMapper.selectPage(page,queryWrapper);
        //当前页集合
        map.put("menuList",aList.getRecords());
        //总页数
        map.put("totalPage",aList.getPages());
        //总条数
        map.put("total",aList.getTotal());
        //当前页
        map.put("curPage",m.getPage());
        if(m.getPage()==1){
            //上一页
            map.put("prePage",1);
        }else{
            map.put("prePage",m.getPage()-1);
        }
        if(m.getPage()==aList.getPages()){
            //下一页
            map.put("nextPage",aList.getPages());
        }else{
            map.put("nextPage",m.getPage()+1);
        }
        //每页显示条数
        map.put("row",m.getRow());
        return map;
    }
}
